<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            height: 4000px;
        }
        #button{
            width: 30px;
            height: 30px;
            text-align: center;
            color: #fff;
            padding: 20px;
            background: rgba(0, 0, 0, 0.5);
            font-size: 14px;
            user-select: none;
            position: fixed;
            right: 0;
            top: 70%;
            display: none;
        }
    </style>
</head>
<body>
    <div id="button">回到顶部</div>
</body>
<script>
    window.onscroll = function(){
        // var top = document.documentElement.scrollTop || document.body.scrollTop;
        var top = window.scrollY;
        if(top>=1500){
            button.style.display = "block";
        }else{
            button.style.display = "none";
        }
    }
    var timer;
    button.onclick = function(){
        // window.scrollTo({top:0,behavior:"smooth"})  
        timer = setInterval(function(){
            window.scrollTo({top:window.scrollY-100})
            if(window.scrollY<=0){
            clearInterval(timer)
        }
        },100)
    }
</script>
</html>